package com.springboot.cloud.demos.producer.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.springboot.cloud.demos.entity.param.AddressInfoQueryParam;
import com.springboot.cloud.demos.entity.po.AddressInfo;
import com.springboot.cloud.demos.producer.dao.AddressInfoMapper;
import com.springboot.cloud.demos.producer.service.IAddressInfoService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
@RefreshScope
public class AddressInfoerviceImpl extends ServiceImpl<AddressInfoMapper, AddressInfo> implements IAddressInfoService {

    @Value("${producer.addressInfo:123}")
    private String value;

    @Override
    @CacheEvict(value = "addressInfo",allEntries=true)
    public boolean add(AddressInfo addressInfo) {
        return this.save(addressInfo);
    }

    @Override
    @CacheEvict(value = "addressInfo", key = "#root.targetClass+'-'+#id")
    public boolean delete(String id) {
        return this.removeById(id);
    }

    @Override
    @CacheEvict(value = "addressInfo", key = "#root.targetClass+'-'+#addressInfo.id")
    public boolean update(AddressInfo addressInfo) {
        return this.updateById(addressInfo);
    }

    @Override
    @Cacheable(value = "addressInfo", key = "#root.targetClass+'-'+#id")
    public AddressInfo get(String id) {
        log.info("value:{}", value);
        return this.getById(id);
    }

    @Override
    public List<AddressInfo> query(AddressInfoQueryParam addressInfoQueryParam) {
        QueryWrapper<AddressInfo> queryWrapper = addressInfoQueryParam.build();
        if (StringUtils.isNotEmpty(addressInfoQueryParam.getUserId())) {
            queryWrapper.eq("user_id", addressInfoQueryParam.getUserId());
        }
        if (StringUtils.isNotEmpty(addressInfoQueryParam.getDefaultFlag())) {
            queryWrapper.eq("default_flag", addressInfoQueryParam.getDefaultFlag());
        }
        queryWrapper.orderByDesc("created_time");
        return this.list(queryWrapper);
    }
}
